package com.dc.web.controller;

import com.dc.constant.Constants;
import com.dc.constant.MessageConstant;
import com.dc.dao.ShipDao;
import com.dc.dao.ShipRepairDao;
import com.dc.dao.special.*;
import com.dc.entity.ShipEntity;
import com.dc.entity.ShipRepairEntity;
import com.dc.entity.special.*;
import com.dc.exception.ServiceException;
import com.dc.model.AjaxResult;
import com.dc.model.WorkflowListBody;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

@Api(tags = "特殊四条船-单船工序")
@RequestMapping("/api/workflow_special")
@RestController
public class WorkflowSpecialController {

    @Resource
    ShipRepairDao shipRepairDao;

    @Resource
    ShipDao shipDao;

    @Resource
    WorkflowSpecial1ElectricBoxDao workflowSpecial1ElectricBoxDao;

    @Resource
    WorkflowSpecial1HouseSandDao workflowSpecial1HouseSandDao;

    @Resource
    WorkflowSpecial1MachinePipeDao workflowSpecial1MachinePipeDao;

    @Resource
    WorkflowSpecial1OtherOilCarDao workflowSpecial1OtherOilCarDao;

    @Resource
    WorkflowSpecial1OtherOilShipDao workflowSpecial1OtherOilShipDao;

    @Resource
    WorkflowSpecial1PaintHouseDao workflowSpecial1PaintHouseDao;

    @Resource
    WorkflowSpecial1PaintShipDao workflowSpecial1PaintShipDao;

    @Resource
    WorkflowSpecial1ShipCutDao workflowSpecial1ShipCutDao;

    @Resource
    WorkflowSpecial1ShipOldDao workflowSpecial1ShipOldDao;

    @Resource
    WorkflowSpecial1ShipSandDao workflowSpecial1ShipSandDao;

    @Resource
    WorkflowSpecial1ShipWeldDao workflowSpecial1ShipWeldDao;

    @Resource
    WorkflowSpecial1TowShipOilDao workflowSpecial1TowShipOilDao;

    @Resource
    WorkflowSpecial1WaterCleanDao workflowSpecial1WaterCleanDao;

    @Resource
    WorkflowSpecial2PaintShipDao workflowSpecial2PaintShipDao;

    @Resource
    WorkflowSpecial2ShipCutDao workflowSpecial2ShipCutDao;

    // 0 船体下料
    @GetMapping("/ship_cut/list")
    public AjaxResult shipCutList(@Validated WorkflowListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();

        /////////////////////////////////////////////////
        if((Constants.SHIP_NAME_YASHAN.equals(shipName) && Constants.SHIP_ID_YASHAN.equals(shipId) )||
                (Constants.SHIP_NAME_KANGDI.equals(shipName) && Constants.SHIP_ID_KANGDI.equals(shipId))) {
            Page<WorkflowSpecial1ShipCutEntity> shipCut1Entities = workflowSpecial1ShipCutDao.findByShipRepairEntity(shipRepairEntity, pageRequest);

            return AjaxResult.success(shipCut1Entities);

        } else if( (Constants.SHIP_NAME_YUYOU.equals(shipName) && Constants.SHIP_ID_YUYOU.equals(shipId))
                || (Constants.SHIP_NAME_KAIYUE.equals(shipName) && Constants.SHIP_ID_KAIYUE.equals(shipId)) ) {
            Page<WorkflowSpecial2ShipCutEntity> shipCut2Entities = workflowSpecial2ShipCutDao.findByShipRepairEntity(shipRepairEntity, pageRequest);

            return AjaxResult.success(shipCut2Entities);
        } else {
            throw new ServiceException(MessageConstant.ERR_SHIP_NAME);
        }
    }

    // 1 船体割旧
    @GetMapping("/ship_old/list")
    public AjaxResult shipOldList(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1ShipOldEntity> shipOldEntities = workflowSpecial1ShipOldDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(shipOldEntities);
    }

    // 2 船体焊接
    @GetMapping("/ship_weld/list")
    public AjaxResult shipWeldList(WorkflowListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1ShipWeldEntity> shipWeldEntities = workflowSpecial1ShipWeldDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(shipWeldEntities);

    }

    // 3 机电焊接
    @GetMapping("/machine_pipe/list")
    public AjaxResult machinePipeList(WorkflowListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1MachinePipeEntity> machinePipeEntities = workflowSpecial1MachinePipeDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(machinePipeEntities);
    }

    // 4 拖轮油耗
    @GetMapping("/towship_oil/list")
    public AjaxResult towshipOil(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1TowShipOilEntity> towShipOilEntities = workflowSpecial1TowShipOilDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(towShipOilEntities);
    }

    // 5-0 其它油耗 单船
    @GetMapping("/other_oil_ship/list")
    public AjaxResult otherOilShip(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1OtherOilShipEntity> otherOilShipEntities = workflowSpecial1OtherOilShipDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(otherOilShipEntities);
    }

    // 5-1 其它油耗 高空车
    @GetMapping("/other_oil_car/list")
    public AjaxResult otherOilCar(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1OtherOilCarEntity> otherOilCarEntities = workflowSpecial1OtherOilCarDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(otherOilCarEntities);
    }

    // 6-0 涂装喷漆 舱室及船壳喷漆
    @GetMapping("/paint_ship/list")
    public AjaxResult paintShip(WorkflowListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();

        /////////////////////////////////////////////////
        if((Constants.SHIP_NAME_YASHAN.equals(shipName) && Constants.SHIP_ID_YASHAN.equals(shipId) )||
                (Constants.SHIP_NAME_KANGDI.equals(shipName) && Constants.SHIP_ID_KANGDI.equals(shipId)))  {
            Page<WorkflowSpecial1PaintShipEntity> shipCut1Entities = workflowSpecial1PaintShipDao.findByShipRepairEntity(shipRepairEntity, pageRequest);

            return AjaxResult.success(shipCut1Entities);

        } else if( (Constants.SHIP_NAME_YUYOU.equals(shipName) && Constants.SHIP_ID_YUYOU.equals(shipId))
                || (Constants.SHIP_NAME_KAIYUE.equals(shipName) && Constants.SHIP_ID_KAIYUE.equals(shipId)) ) {
            Page<WorkflowSpecial2PaintShipEntity> shipCut2Entities = workflowSpecial2PaintShipDao.findByShipRepairEntity(shipRepairEntity, pageRequest);

            return AjaxResult.success(shipCut2Entities);
        } else {
            throw new ServiceException(MessageConstant.ERR_SHIP_NAME);
        }
    }

    // 6-1 涂装喷漆 喷漆房分段喷漆
    @GetMapping("/paint_house/list")
    public AjaxResult paintHouse(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1PaintHouseEntity> paintHouseEntities = workflowSpecial1PaintHouseDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(paintHouseEntities);
    }

    // 7-0 打砂除锈 舱室打砂
    @GetMapping("/ship_sand/list")
    public AjaxResult shipSand(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1ShipSandEntity> shipSandEntities = workflowSpecial1ShipSandDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(shipSandEntities);
    }

    // 7-1 打砂除锈 打砂房分段打砂
    @GetMapping("/house_sand/list")
    public AjaxResult houseSand(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1HouseSandEntity> houseSandEntities = workflowSpecial1HouseSandDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(houseSandEntities);
    }

    // 8 超水除锈
    @GetMapping("/water_clean/list")
    public AjaxResult waterClean(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1WaterCleanEntity> waterCleanEntities = workflowSpecial1WaterCleanDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(waterCleanEntities);
    }

    // 9 移动电表
    @GetMapping("/electric_box/list")
    public AjaxResult electricBox(WorkflowListBody body) {
        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        Long shipRepairId = body.getRepairId();

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        Long shipId = shipRepairEntity.getShipId();

        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        String shipName = ship.getName();
        /////////////////////////////////////////////////
        Page<WorkflowSpecial1ElectricBoxEntity> electricBoxEntities = workflowSpecial1ElectricBoxDao.findByShipRepairEntity(shipRepairEntity, pageRequest);
        return AjaxResult.success(electricBoxEntities);
    }

}
